/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.example.common.i18n.api;

import com.example.common.base.exception.GlobalException;
import com.example.common.base.exception.ValidateCodeException;
import com.example.common.i18n.util.MessageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lengleng
 * @date 2018/8/30
 * 全局的的异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 全局异常.
	 *
	 * @param e the e
	 * @return R
	 */
	@ExceptionHandler(ValidateCodeException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public AjaxJson exception(ValidateCodeException e) {
		log.error("ValidateCodeException异常信息 ex={}", e.getMessage(), e);
		return MessageUtils.error(e.getMessage());
	}

	/**
	 * 全局异常.
	 *
	 * @param e the e
	 * @return R
	 */
	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public AjaxJson exception(Exception e) {
		log.error("全局异常信息 ex={}", e.getMessage(), e);
		return MessageUtils.error(e.getMessage());
	}

	/**
	 * 全局GlobalException异常.
	 * @param e the e
	 * @return R
	 */
	@ExceptionHandler(GlobalException.class)
	//正确格式返回前端
	@ResponseStatus(HttpStatus.OK)
	public AjaxJson exception(HttpServletRequest request, GlobalException e) {
		log.error("全局GlobalException信息 ex={}", e.getMessage(), e);
		return MessageUtils.error(e.getMessage());
	}

	/**
	 * TODO 国际化支持
	 * validation Exception
	 *
	 * @param exception
	 * @return R
	 */
	@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
	//正确格式返回前端
	@ResponseStatus(HttpStatus.OK)
	public AjaxJson bodyValidExceptionHandler(HttpServletRequest request, MethodArgumentNotValidException exception) {
		List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
		//TODO 国际化支持
		String key = fieldErrors.get(0).getDefaultMessage();
		return MessageUtils.error(key);
	}

}
